package com.shd.edanpei.order;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.ArrayRes;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseQuickImageAdapter;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by mc on 2018/11/1.
 * 选择item Dialog
 */

public class ItemDialog extends Dialog {

    private final int COLORS[] = {R.color.blue, R.color.black};
    private final int IMGS[] = {R.drawable.ic_all_choose_blue, R.drawable.ic_all_unchoose};

    private Context mContext;
    private ItemAdapter mAdapter;
    private String mTitle;
    private int mArrayID;
    private Listener mListener;
    private String mInfo;

    private TextView tvTitle, tvCancel, tvSure;

    public ItemDialog(@NonNull Context context, String title, @ArrayRes int arrayID, Listener listener) {
        super(context);
        this.mContext = context;
        this.mTitle = title;
        this.mArrayID = arrayID;
        this.mListener = listener;
        initView();
    }


    private void initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_dialog, null);
        this.setContentView(view);
        setCanceledOnTouchOutside(false);

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        tvTitle = findViewById(R.id.tv_title);
        tvCancel = findViewById(R.id.tv_cancel);
        tvSure = findViewById(R.id.tv_sure);

        tvTitle.setText(mTitle);

        mAdapter = new ItemAdapter(mContext);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter);
        String[] stringArray = mContext.getResources().getStringArray(mArrayID);
        List<InfoBean> list = new ArrayList<>();
        for (String s : stringArray) {
            list.add(new InfoBean(s, false));
        }

        if (list.size() > 0) {
            mAdapter.setNewData(list);
        }
        initListener();
    }

    private void initListener() {
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < mAdapter.getData().size(); i++) {
                    if (position == i) {
                        mAdapter.getItem(i).setCheck(true);
                        mInfo = mAdapter.getItem(i).getInfo();
                    } else {
                        mAdapter.getItem(i).setCheck(false);
                    }
                    mAdapter.notifyDataSetChanged();
                }

            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(mInfo)) {
                    mListener.setInfo(mInfo);
                    dismiss();
                }
            }
        });
    }

    public void setListener(Listener listener) {

        this.mListener = listener;
    }

    public interface Listener {

        void setInfo(String str);
    }


    private class ItemAdapter extends BaseQuickImageAdapter<InfoBean> {


        ItemAdapter(Context context) {
            super(context, R.layout.item_radio);
        }

        @Override
        protected void convert(BaseViewHolder helper, InfoBean item) {
            helper.setText(R.id.text, item.getInfo());
            if (item.isCheck()) {
                helper.setTextColor(R.id.text, mContext.getResources().getColor(COLORS[0]));
                ((ImageView) helper.getView(R.id.img)).setImageResource(IMGS[0]);
            } else {
                helper.setTextColor(R.id.text, mContext.getResources().getColor(COLORS[1]));
                ((ImageView) helper.getView(R.id.img)).setImageResource(IMGS[1]);
            }
        }
    }

    private class InfoBean {
        private String info;
        private boolean isCheck;

        InfoBean(String info, boolean isCheck) {
            this.info = info;
            this.isCheck = isCheck;
        }

        public String getInfo() {
            return info;
        }

        boolean isCheck() {
            return isCheck;
        }

        void setCheck(boolean check) {
            isCheck = check;
        }
    }

}
